package com.example.binpacking.util;

import com.example.binpacking.model.*;
import com.example.binpacking.model.wrapper.FunInterviewWrapperContent;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class DataFormatter {

    public static Map<String, Object> formatData(Object entity) {
        Map<String, Object> formattedData = new HashMap<>();
        if (entity instanceof Interviewer) {
            Interviewer interviewer = (Interviewer) entity;
            formattedData.put("loginid", interviewer.getLoginid().toString() );
            formattedData.put("password", interviewer.getPassword() );
            formattedData.put("name",  interviewer.getName() );
            formattedData.put("phone", interviewer.getPhone() == null ? "" : interviewer.getPhone());
            formattedData.put("email", interviewer.getEmail() == null ? "" : interviewer.getEmail());
            formattedData.put("parentid",  interviewer.getParentid().toString() );
            formattedData.put("orderid", interviewer.getOrderid().toString() );
            formattedData.put("isdeleted",  interviewer.getIsdeleted().toString() );
            formattedData.put("createdate",  interviewer.getCreatedate().toString() );
            System.out.println(interviewer.getCreatedate().toString());
        } else if (entity instanceof Teacher) {
            Teacher teacher = (Teacher) entity;
            // Similar conversion logic for Teacher fields
        } else if (entity instanceof Sysmaster) {
            Sysmaster sysmaster = (Sysmaster) entity;
            // Similar conversion logic for Sysmaster fields
        }
        return formattedData;
    }

    public static Map<String, Object> formatDataByNull(Object entity) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            for (Field field : entity.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                Object value = field.get(entity);
                if (value == null) {
                    resultMap.put(field.getName(), "");
                } else {
                    resultMap.put(field.getName(), value.toString());
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return resultMap;
    }


    // 将集合中的每个实体转换为Map<String, Object>类型，属性值为null的改为空双引号""
    public static <T> List<Map<String, Object>> convertAndReturnMapList(Collection<T> entities) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (T entity : entities) {
            resultList.add(convertEntityToMap(entity));
        }
        return resultList;
    }

    // 将实体转换为Map<String, Object>，属性值为null的改为空双引号""
    private static <T> Map<String, Object> convertEntityToMap(T entity) {
        Map<String, Object> resultMap = new HashMap<>();
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(entity);
                if (value == null) {
                    if (field.getType().equals(String.class)) {
                        resultMap.put(field.getName(), "");
                    } else {
                        resultMap.put(field.getName(), value);
                    }
                } else {
                    resultMap.put(field.getName(), value);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return resultMap;
    }
    public static List<FunInterviewWrapperContent> formatData(List<FunInterview> funInterviews, List<InterviewContent> interviewContents) {
        List<FunInterviewWrapperContent> result = new ArrayList<>();
        for (FunInterview funInterview : funInterviews) {
            FunInterviewWrapperContent funInterviewWrapper = new FunInterviewWrapperContent(funInterview.getLoginid(), funInterview.getRightid(),funInterview.getId(),funInterview.getBegindate(),
                    funInterview.getEnddate(),funInterview.getIsdeleted(),funInterview.getIscalculated(), funInterview.getQnum(),funInterview.getCalculatedtip(),funInterview.getName(),interviewContents);
            result.add(funInterviewWrapper);
        }
        return result;
    }


    public static List<Map<String, Object>> convertAndReturnMapList(List<Interview> interviews) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return interviews.stream().map(interview -> {
            Map<String, Object> map = new HashMap<>();
            map.put("loginid", interview.getLoginid());
            map.put("begindate", interview.getBegindate() != null ? interview.getBegindate() : "");
            map.put("enddate", interview.getEnddate() != null ? interview.getEnddate() : "");
            map.put("iscalculated", interview.getIscalculated() != null ? interview.getIscalculated() : "");
            map.put("orderid", interview.getOrderid() != null ? interview.getOrderid().toString() : "");
            map.put("qnum", interview.getQnum() != null ? interview.getQnum(): "");
            map.put("rightid", interview.getRightid() != null ? interview.getRightid().toString() : "");
            map.put("id", interview.getId() != null ? interview.getId().toString() : "");
            map.put("isdeleted", interview.getIsdeleted() != null ? interview.getIsdeleted() : "");
            return map;
        }).collect(Collectors.toList());
    }

    public static Map<String, Object> formatInterviewData(Map<String, Object> baseData, List<Map<String, Object>> interviewData) {
        baseData.put("content", interviewData);
        return baseData;
    }

    public static Map<String, Object> convertToMap(Object obj) {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = obj.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            try {
                Object value = field.get(obj);
                // 将 null 值转换为空字符串
                if (value == null) {
                    value = "";
                } else if ( value instanceof Number || value instanceof Boolean) {
                    value = value.toString();
                }
                map.put(field.getName(), value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return map;
    }

    public static List<Map<String, Object>> convertAndReturnMapListOne(List<?> list) {
        return list.stream()
                .map(DataFormatter::convertToMap)
                .collect(Collectors.toList());
    }

}
